package com.maxh.controller;

import com.maxh.annotation.AdminLoginToken;
import com.maxh.annotation.UserLoginToken;
import com.maxh.pojo.Attribute;
import com.maxh.pojo.Feedback;
import com.maxh.service.FeedbackService;
import com.maxh.util.ReturnData;
import com.maxh.util.ReturnError;
import com.maxh.util.ReturnUtil;
import com.maxh.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;

    /*添加意见反馈*/
    @UserLoginToken
    @PostMapping("/training/student/feedback/add")
    public Object addFeedback(Feedback feedback) throws ParseException {
        if(feedback.getContent()==null || feedback.getName()==null || feedback.getMobile()==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        /*当前时间*/
        Date dateTime = new ReturnUtil().dateTime();
        feedback.setCreateTime(dateTime);
        feedback.setStatus(1);
        System.out.println("添加意见反馈："+feedback);
        Boolean isAdd = feedbackService.addFeedback(feedback);
        System.out.println(isAdd);
        if(isAdd){
            //根据id获取课程属性
            return new ReturnData(StatusCode.REQUEST_SUCCESS, feedback,"信息反馈成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"信息反馈失败");
        }
    }

    /*管理员获取反馈信息*/
    @AdminLoginToken
    @GetMapping("/training/admin/feedback/list")
    public Object feedbackList(){
        List feedbackList = (List) feedbackService.feedbackList();
        System.out.println("反馈信息"+feedbackList);
        if(feedbackList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,feedbackList ,"后端数据交互正常！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,feedbackList,"数据为空");
        }
    }
}
